package com.zhuiyun.project.rabbitmq;

import com.rabbitmq.client.Channel;
import com.zhuiyun.project.api.coachApply.entity.CoachApply;
import com.zhuiyun.project.api.coachApply.mapper.CoachApplyMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Arrays;

import static com.zhuiyun.project.config.RabbitMqConfig.DEAD_QUEUE;

/**
 * @Description TODO  RabbitMq消费者
 * @Author shy
 * @Date 2024/3/9 20:45
 */
@Component
public class LiveConsumer {
    @Resource
    RabbitTemplate rabbitTemplate;
    @Resource
    private CoachApplyMapper coachApplyMapper;
    /**
     *
     * @Author shy  死信队列监听
     * @Date 2024/3/9 20:51
      * @param message

     */
    @RabbitListener(queues = DEAD_QUEUE)
    public void DeadLiveListener(Message message, Channel channel) throws IOException {
        String CoachId=new String(message.getBody());
        System.out.println("邀请码id"+CoachId);
        //根据消息判断 邀请码状态
        CoachApply coachApply = coachApplyMapper.getCoachApplyStatus(CoachId);
        //判断状态是否为拒绝
        if(coachApply.getApplyStatus()==0||coachApply.getApplyStatus()==1){
            //超时 拒绝该邀请
            int i=coachApplyMapper.updateStatusById(coachApply.getId());
            if(i>0){
                //消费成功                 消息唯一标识                              false 确认当前只有一个消息收到
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            }else {
                //消费失败重回队列       消息唯一标识                                 是否重回队列
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            }
        }else {
            //教练已通过
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }
    }
}